package com.ludomancy.gamelib;

import java.applet.AudioClip;
import java.awt.Image;
import java.util.Hashtable;

public class ResourceManager
{
    private static Hashtable images = new Hashtable();
    private static Hashtable sounds = new Hashtable();
    private static Hashtable tracks = new Hashtable();
    private static ResourceLoader imgLoader;
    private static String basePathString;
     
    
    public static void init(ResourceLoader loader, String basePath)
    {
    	images.clear();
    	sounds.clear();
    	tracks.clear();
    	imgLoader = loader;
    	basePathString = basePath;
    }
    
    public static Music getTrack(String name)
    {
        Music img = (Music) tracks.get(name);
        if(img == null)
        {
        	img = new Music(name);
           	tracks.put(name, img);
        }
        return img;
    }
    
    public static Image getImage(String name)
    {
        Image img = (Image) images.get(name);
        if(img == null)
        {
        	img = imgLoader.loadImage(basePathString+name);
           	images.put(name, img);
        }
        return img;
    }
    
    static AudioClip getSound(String file)
    {
        AudioClip clip = (AudioClip) sounds.get(file);
        if(clip == null)
        {
            try
            {
            	clip = imgLoader.loadClip(basePathString+file);
            	clip.play();
            	clip.stop();
                sounds.put(file, clip);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return clip;
    }
}
